#ifndef __CODE__
#define __CODE__

#include "city.h"
#include <math.h>
#include <iostream>
#include <cstdlib>

class code{
 private:
  int *array;
  int length;
 public:
  code(){};
  code(int *_A, int _l){
    array =_A;
    length = _l;
  }
  ~code(){};
  int get_length() const;
  int *get_array();
  void set_array(int *_idx);
  double total_dis(const city* C, int _l);
  double fitness(const city*C, int _l, double _max);
  void print_out() const;
};

int code::get_length() const{
  return length;
}

int* code::get_array(){
  return array;
}

void code::set_array(int* _idx){
  array = _idx;
}

double code::total_dis(const city* C, int _l){
  double sum=0;
  double x1,y1;
  for (int i=1; i<_l; i++){
    x1=C[array[i]].get_x()-C[array[i-1]].get_x();
    y1=C[array[i]].get_y()-C[array[i-1]].get_y();
    sum+=sqrt(x1*x1+y1*y1);
  }
  return sum;
}

double code::fitness(const city*C, int _l ,double _max){
  double f=(*this).total_dis(C,_l);
  return _max+2-f;
}

void code::print_out() const{
  std::cout << "[";
  for (int i=0; i<length ;i++)
    std::cout << array[i] << ",";
  std::cout << "\b]" << std::endl;
}

#else
// Do nothing.
#endif
